package avicit.bdp.dgs.qa.rest;

import avicit.bdp.dgs.qa.dto.RuleDTO;
import avicit.bdp.dgs.qa.service.RuleService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-03-30
 * @类说明：RuleRest
 * @修改记录：
 * @注意事项：
 * @主要功能：质量规则Rest接口
 */
@RestController
@Api(tags = "qa", description = "质量规则Rest接口")
@RequestMapping("/api/bdp/dgs/qa/rule")
public class RuleRest {
    @Autowired
    private RuleService ruleService;

    /**
     * 新增质量规则
     *
     * @param ruleDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增质量规则")
    public ResponseMsg<String> addRule(@RequestBody RuleDTO ruleDTO) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleService.addRule(ruleDTO));

        return responseMsg;
    }

    /**
     * 删除/批量删除质量规则，多个id使用英文逗号隔开
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除/批量删除质量规则，多个id使用英文逗号隔开")
    public ResponseMsg<Integer> deleteRule(@ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleService.deleteRule(id));

        return responseMsg;
    }

    /**
     * 修改质量规则
     *
     * @param ruleDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改质量规则")
    public ResponseMsg<Boolean> updateRule(@RequestBody RuleDTO ruleDTO) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleService.updateRule(ruleDTO));

        return responseMsg;
    }

    /**
     * 查询质量规则详情
     *
     * @param id 质量规则ID
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "查询质量规则详情")
    public ResponseMsg<RuleDTO> getRuleDetail(@ApiParam(value = "id", name = "id") @RequestParam String id) {
        ResponseMsg<RuleDTO> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleService.getRuleDetail(id));

        return responseMsg;
    }

    /**
     * 分页查询规则
     *
     * @param keyWords
     * @param measureType
     * @param templateType
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/query")
    @ApiOperation(value = "分页查询质量规则")
    public ResponseMsg<QueryRespBean<RuleDTO>> queryRule(
            @ApiParam(value = "作业Id", name = "jobId") @RequestParam String jobId,
            @ApiParam(value = "查询关键字", name = "keyWords") @RequestParam(required = false) String keyWords,
            @ApiParam(value = "监控类型", name = "measureType") @RequestParam(required = false) Integer measureType,
            @ApiParam(value = "模板类型", name = "templateType") @RequestParam(required = false) Integer templateType,
            @ApiParam(value = "当前页", name = "pageNo") @RequestParam(defaultValue = "1", required = false) Integer pageNo,
            @ApiParam(value = "每页大小", name = "pageSize") @RequestParam(defaultValue = "10", required = false) Integer pageSize) {
        ResponseMsg<QueryRespBean<RuleDTO>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleService.queryRule(jobId, keyWords, measureType, templateType, pageNo, pageSize));

        return responseMsg;
    }

    /**
     * 判读规则名称是否重复
     *
     * @param jobId  作业ID
     * @param ruleId 规则ID，规则编辑时修改名称，需要传入ruleId;新建场景不需要传入
     * @param name   规则名称
     * @return
     */
    @GetMapping("/isSameName")
    @ApiOperation(value = "判读质量作业名称是否重复")
    public ResponseMsg<Boolean> isSameName(@ApiParam(value = "作业ID", name = "jobId") @RequestParam String jobId,
                                           @ApiParam(value = "规则ID", name = "ruleId") @RequestParam(required = false) String ruleId,
                                           @ApiParam(value = "名字", name = "name") @RequestParam String name) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleService.isSameName(jobId, ruleId, name));

        return responseMsg;
    }

    /**
     * 批量设置权重
     *
     * @param rules
     * @return
     */
    @PostMapping("/weight")
    @ApiOperation(value = "修改质量规则")
    public ResponseMsg<Boolean> setRuleWeight(@RequestBody List<RuleDTO> rules) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleService.setRuleWeight(rules));

        return responseMsg;
    }

    /**
     * 预览质量规则SQL
     *
     * @param ruleDTO
     * @return
     */
    @PostMapping("/previewSql")
    @ApiOperation(value = "查询质量规则详情")
    public ResponseMsg<String> previewSql(@RequestBody RuleDTO ruleDTO) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleService.previewSql(ruleDTO));

        return responseMsg;
    }

    /**
     * 验证SQL语法合法性
     *
     * @param ruleDTO
     * @return
     */
    @PostMapping("/validSql")
    @ApiOperation(value = "验证SQL语法合法性")
    public ResponseMsg<Boolean> validSql(@RequestBody RuleDTO ruleDTO) {
        ResponseMsg responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.ruleService.validSql(ruleDTO));

        return responseMsg;
    }
}
